package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.util.StringUtil;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.RoomLiveNormalActivity;
import com.imay.live.activity.discovery.DicLoadActivity;
import com.imay.live.activity.discovery.HotLabelDetailActivity;
import com.imay.live.activity.family.controller.FamilyLiveController;
import com.imay.live.adapter.DiscoveryAdapter;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/11 0011.
 */

public class FamilyLiveActivity extends DicLoadActivity {
    public static final String KEY_FAMILY_ID = "key_family_id";

    private int count = 30;
    private int start;

    private String loadLiveFamilyRefreshCommand, loadLiveFamilyloadmoreCommand;

    private int familyId;

    /**
     * @param context
     */
    public static void startActivity(Context context, int familyid) {
        Intent intent = new Intent(context, FamilyLiveActivity.class);
        intent.putExtra(KEY_FAMILY_ID, familyid);
        context.startActivity(intent);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(loadLiveFamilyRefreshCommand)) {
            try {
                Result<JSONObject> result = JSON.parseObject(data, new TypeReference<Result<JSONObject>>() {
                }.getType());
                if (result.data.containsKey("rooms")) {
                    ArrayList<RoomInfoModel> liveData = JSON.parseObject(result.data.getString("rooms"), new TypeReference<List<RoomInfoModel>>() {
                    }.getType());
                    loadViewControl.refreshLoadView(liveData, result.result);
                    start = liveData.size();
                }
            } catch (Exception e) {
                loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
            }
        } else if (command.equals(loadLiveFamilyloadmoreCommand)) {
            try {
                Result<JSONObject> result = JSON.parseObject(data, new TypeReference<Result<JSONObject>>() {
                }.getType());
                if (result.data.containsKey("rooms")) {
                    ArrayList<RoomInfoModel> liveData = JSON.parseObject(result.data.getString("rooms"), new TypeReference<List<RoomInfoModel>>() {
                    }.getType());
                    loadViewControl.moreLoadView(liveData, result.result);
                    start = start + liveData.size();
                }
            } catch (Exception e) {
                loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        loadViewControl.loadError(message);
    }

    private String loadLiveFamily() {
        return ProtoRequestHelper.task(ProtoHttpFactory.getFamilyLive(familyId, start, count)).setTag(this.hashCode()).request(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mrecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    }

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        familyId = getIntent().getIntExtra(KEY_FAMILY_ID, -1);
        setTitle(R.string.str_family_person_live);
        ViewUtil.setForgroundRadius(ViewUtil.findView(this, R.id.container));

        onRefreshLoad();
    }

    @Override
    public void onRefreshLoad() {
        start = 0;
        loadLiveFamilyRefreshCommand = loadLiveFamily();
    }

    @Override
    public void onLoadMore() {
        loadLiveFamilyloadmoreCommand = loadLiveFamily();
    }

    @Override
    protected void initAdapte() {
        super.initAdapte();
        count = 30;
        mAdapter = new DiscoveryAdapter(mdatas);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.discover_live_rl_square) {
                    RoomLiveNormalActivity.startActivity(FamilyLiveActivity.this, (ArrayList<RoomInfoModel>) adapter.getData(), position);
                }
            }
        });
    }

}
